import os.path
import re
import requests
from lxml import etree
import execise_proxy_ip
import random

url_index = "https://dandanzan.net"
url_movie_item_1 = "/fetch_plays/"
url_movie_item_2 = "/chao_qing"
url_lib = "https://baidu.sd-play.com/"


# 获取电影排行榜列表
def get_movie_list():
    title_and_id_list = {}
    res = requests.get(url_index, proxies={"http": random.choice(ips)})
    selector = etree.HTML(res.text)
    url_list = selector.xpath(
        '//div[@class="sidebar"]/div[@class="lists lists-rank lists-title-lines-1"]/div[@class="lists-content"]/ul/li/h2/a')

    for titleAndHref in url_list:
        movie_name = titleAndHref.xpath("./@title")[0]
        movie_url = titleAndHref.xpath("./@href")[0]
        movie_id = (os.path.basename(movie_url))[0:-5]
        title_and_id_list[movie_name] = movie_id
    return title_and_id_list


# 获取所有电影的超清列表链接
def get_chaoqing_movie_list(title_and_id_list):
    title_chaoqing_href_list = []
    title_and_id_and_video_plays = {}
    for movie_title, movie_id in title_and_id_list.items():
        list_url = url_index + url_movie_item_1 + str(movie_id) + url_movie_item_2
        res = requests.get(list_url, proxies={"http": random.choice(ips)})
        if not res.status_code == 200:
            continue

        if not os.path.exists(movie_title):
            os.makedirs(movie_title)
        ret = eval(res.text)
        title_and_id_and_video_plays["video_plays"] = ret.get("video_plays")
        title_and_id_and_video_plays["movie_name"] = movie_title
        title_and_id_and_video_plays["movie_id"] = movie_id
        title_chaoqing_href_list.append(title_and_id_and_video_plays)

        # 测试
        break
    return title_chaoqing_href_list


# 获取视频的index_m3u8接口的返回值
def get_index_m3u8(title_chaoqing_href_list):
    for play_date_and_src_site_and_movie_name in title_chaoqing_href_list:
        movie_name = play_date_and_src_site_and_movie_name["movie_name"]
        movie_id = play_date_and_src_site_and_movie_name["movie_id"]
        for play_date_and_src_site in play_date_and_src_site_and_movie_name["video_plays"]:
            play_data = play_date_and_src_site["play_data"]
            src_site = play_date_and_src_site["src_site"]
            res = requests.get(play_data, proxies={"http": random.choice(ips)})

            m3u8 = re.findall("/\d.*?/\w.*?/\w.*?/\w.*?/index.m3u8", res.text, re.S)[0]
            index_m3u8 = url_lib + m3u8

            res = requests.get(index_m3u8, proxies={"http": random.choice(ips)})
            with open(f"{movie_name}/index.m3u8", "wb") as f:
                f.write(res.content)
            break


def get_all_ts_by_index_m3u8(title_chaoqing_href_list):
    for title_and_id in title_chaoqing_href_list:
        movie_name = title_and_id["movie_name"]
        with open(f"{movie_name}/index.m3u8") as f:
            m3u8 = f.read()
        ts_list = re.findall("https:.*?\.ts", m3u8)
        key = re.findall("https:.*?\.key", m3u8)[0]
        key_name = os.path.basename(key)
        if not os.path.exists(f"{movie_name}/{key_name}"):
            res = requests.get(key,proxies={"http": random.choice(ips)})
            with open(f"{movie_name}/{key_name}", "wb") as f:
                f.write(res.content)

        for ts in ts_list:
            ts_name = os.path.basename(ts)
            if os.path.exists(f"{movie_name}/{ts_name}"):
                continue
            res = requests.get(ts,proxies={"http": random.choice(ips)})
            with open(f"{movie_name}/{ts_name}", "wb") as f:
                f.write(res.content)
                print(f"{ts_name}写入完成")


def ts_to_mp4_by_ffmpeg(title_chaoqing_href_list):

    for title_and_id in title_chaoqing_href_list:
        movie_name = title_and_id["movie_name"]
        with open(f"{movie_name}/index.m3u8", "r") as f:
            m3u8 = f.read()
        m3u8=m3u8.replace("https://baidu.shanshanku.com/20221118/8Z5fU368/1200kb/hls/", "")
        with open(f"{movie_name}/index.m3u8", "w") as f:
            f.write(m3u8)
        print(m3u8)
        os.chdir(f"{movie_name}")
        os.system(f'ffmpeg -allowed_extensions ALL -protocol_whitelist "file,http,crypto,tcp" -i index.m3u8 -c copy {movie_name}\.mp4')
        print(movie_name + "下载完毕")


# 获取视频的列表
if __name__ == '__main__':
    ips = execise_proxy_ip.get_ips()
    # 获取视频排行榜
    title_and_id_list = get_movie_list()
    # 获取所有电影的超清列表链接
    title_chaoqing_href_list = get_chaoqing_movie_list(title_and_id_list)
    # 从index.m3u8中获取电影的m3u8的列表，写到文件中
    get_index_m3u8(title_chaoqing_href_list)
    # 根据文件获取全部的ts文件
    get_all_ts_by_index_m3u8(title_chaoqing_href_list)
    # 用ffmpeg去合并ts为一个mp4文件
    ts_to_mp4_by_ffmpeg(title_chaoqing_href_list)

